package com.zjt.myjiakao;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.google.gson.Gson;
import com.zjt.myjiakao.error.ErrorActivity;
import com.zjt.myjiakao.gdt.UtilDialog;
import com.zjt.myjiakao.pojo.AllErrorPoJp;
import com.zjt.myjiakao.pojo.AllListData;
import com.zjt.myjiakao.pojo.KeMuYiPoJo;
import com.zjt.myjiakao.util.FileUtil2;

import org.litepal.LitePal;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    String   json;
    KeMuYiPoJo poJo;
    private Button kemuyi;
    private Button rand;
    private Button error;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        json=FileUtil2.getJson("kemu1.json", this);
        poJo=new Gson().fromJson(json,KeMuYiPoJo.class);
        Log.e("123",poJo.getResult().size()+"");
        initView();
        startKemuYi();
        startRand();
        startError();
        //模拟测试数据
        testData();
        //得到模拟数据
        //getData();
    }

    private void getData() {
        AllListData data= testData();
        Log.e("111",data.getTitle());
        for (int i=0;i<data.getList().size();i++){
            Log.e("123",data.getList().get(i).getItem1());
        }
    }

    private AllListData testData() {
        String json=FileUtil2.getJson("first.json", this);
        KeMuYiPoJo bean=new Gson().fromJson(json,KeMuYiPoJo.class);
        AllListData data=new AllListData();
        data.setList(bean.getResult());
        data.setTitle("道路交通安全法律、法规和规章");
        return data;
    }

    private void startError() {
        error.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,ErrorActivity.class));
            }
        });
    }

    private void startRand() {
        rand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,RandActivity.class));
            }
        });
    }

    private void startKemuYi(){
        kemuyi.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,ReadActivity.class));
            }
        });
    }

    private void initView() {
        kemuyi=findViewById(R.id.kemuyi);
        rand=findViewById(R.id.rand);
        error=findViewById(R.id.error);
        //        List<AllErrorPoJp> allSongs = LitePal.findAll(AllErrorPoJp.class);
//        for (AllErrorPoJp x: allSongs){
//            Log.e("888",x.getQuestion());
//        }
    }

    @Override
    public void onBackPressed() {
        UtilDialog.showExitAppDialog(MainActivity.this);
    }
}
